/**
 * 精灵视图组件
 * 
 * 职责：
 * 1. 缓存 Sprite 相关组件
 * 2. 提供 Sprite 渲染的便捷方法
 * 
 * 适用场景：
 * - 纯图片实体（敌人、道具、障碍物）
 * - 不需要文本的 2D 对象
 * 
 * 内存占用：~56 bytes
 * - 基类: ~40 bytes
 * - _sprite: 8 bytes
 * - _uiTransform: 8 bytes
 * 
 * @author AI Assistant
 * @date 2025-11-29
 */

import { ecs } from "../../../../core/ecs/ECS";
import { Sprite, UITransform, Color } from "cc";
import { ViewComponentBase } from "./ViewComponentBase";

/**
 * 精灵视图组件（只缓存 Sprite）
 */
@ecs.register('ViewSprite', false)
export class ViewComponentSprite extends ViewComponentBase {
    // ========== 懒加载缓存 ==========
    
    /** Sprite 组件（图片渲染）- 懒加载 */
    private _sprite: Sprite | null | undefined = undefined;
    
    /** UITransform 组件（UI 变换）- 懒加载 */
    private _uiTransform: UITransform | null | undefined = undefined;
    
    // ========== 懒加载 Getter ==========
    
    /**
     * 获取 Sprite 组件（懒加载）
     */
    get sprite(): Sprite | null {
        if (this._sprite === undefined) {
            this._sprite = this.node?.getComponent(Sprite) || null;
        }
        return this._sprite;
    }
    
    /**
     * 获取 UITransform 组件（懒加载）
     */
    get uiTransform(): UITransform | null {
        if (this._uiTransform === undefined) {
            this._uiTransform = this.node?.getComponent(UITransform) || null;
        }
        return this._uiTransform;
    }
    
    /**
     * 重置组件
     */
    reset(): void {
        super.reset();
        this._sprite = undefined;
        this._uiTransform = undefined;
    }
    
    // ========== 便捷方法 ==========
    
    /**
     * 设置精灵颜色
     */
    setColor(color: Color): void {
        if (this.sprite) {
            this.sprite.color = color;
        }
    }
    
    /**
     * 设置精灵透明度
     */
    setOpacity(opacity: number): void {
        if (this.sprite) {
            const color = this.sprite.color.clone();
            color.a = opacity;
            this.sprite.color = color;
        }
    }
    
    /**
     * 设置精灵帧（SpriteFrame）
     */
    setSpriteFrame(spriteFrame: any): void {
        if (this.sprite) {
            this.sprite.spriteFrame = spriteFrame;
        }
    }
    
    /**
     * 淡入效果
     */
    fadeIn(duration: number, callback?: () => void): void {
        if (!this.sprite) return;
        
        const startOpacity = this.sprite.color.a;
        const targetOpacity = 255;
        const startTime = Date.now();
        
        const update = () => {
            const elapsed = Date.now() - startTime;
            const progress = Math.min(elapsed / (duration * 1000), 1);
            const opacity = startOpacity + (targetOpacity - startOpacity) * progress;
            
            this.setOpacity(opacity);
            
            if (progress < 1) {
                requestAnimationFrame(update);
            } else if (callback) {
                callback();
            }
        };
        
        update();
    }
    
    /**
     * 淡出效果
     */
    fadeOut(duration: number, callback?: () => void): void {
        if (!this.sprite) return;
        
        const startOpacity = this.sprite.color.a;
        const targetOpacity = 0;
        const startTime = Date.now();
        
        const update = () => {
            const elapsed = Date.now() - startTime;
            const progress = Math.min(elapsed / (duration * 1000), 1);
            const opacity = startOpacity + (targetOpacity - startOpacity) * progress;
            
            this.setOpacity(opacity);
            
            if (progress < 1) {
                requestAnimationFrame(update);
            } else if (callback) {
                callback();
            }
        };
        
        update();
    }
}

